@extends('layouts.admin.application')
@section('css')
    <style>
        .thumb {
            max-height: 60px;
        }
    </style>
@endsection
@section('content')
    <div class="admin-content">

        <div class="am-cf am-padding">
            <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">章节列表</strong> /
                <small>Episode List</small>
            </div>
        </div>

        @include('layouts.admin.partials._flash')

        <div class="am-g">
            <div class="am-u-sm-12 am-u-md-6">
                <div class="am-btn-toolbar">
                    <div class="am-btn-group am-btn-group-xs">
                        <button type="button" class="am-btn am-btn-default" id="destroy_checked">
                            <span class="am-icon-trash-o"></span> 删除
                        </button>
                    </div>
                </div>
            </div>

            <div class="am-u-sm-12 am-u-md-3">
                <div class="am-form-group">
                    <select data-am-selected="{btnWidth: '80%',  btnStyle: 'secondary', btnSize: 'sm', maxHeight: 360, searchBox: 1}" name="category_id" id="change_category">
                        <option value="">请选择分类</option>
                        @foreach ($categories as $category)
                            <optgroup label="{{ $category->name }}">
                                @foreach ($category->children as $c)
                                    <option value="{{ $c->id }}">
                                        {{ $c->name }}
                                    </option>
                                @endforeach
                            </optgroup>
                        @endforeach
                    </select>
                </div>
            </div>

            <div class="am-u-sm-12 am-u-md-3">
                <form action="{{route('casts.episode.index')}}" method="get">
                    <div class="am-input-group am-input-group-sm">
                        <input type="text" class="am-form-field" name="keyword" value="{{Request::input('keyword')}}">
                        <span class="am-input-group-btn">
                            <button class="am-btn am-btn-default" type="submit">搜索</button>
                        </span>
                    </div>
                </form>
            </div>
        </div>

        <div class="am-g">
            <div class="am-u-sm-12">
                <form class="am-form" id="form">
                    <table class="am-table am-table-striped am-table-hover table-main">
                        <thead>
                        <tr>
                            <th class="table-check"><input type="checkbox" id="checked"/></th>
                            <th class="table-id">编号</th>
                            <th class="table-title">名称</th>
                            <th>作者</th>
                            <th class="table-category">所属分类</th>
                            <th class="table-date am-hide-sm-only">发布日期</th>
                            <th class="table-top am-hide-sm-only">是否免费</th>
                            <th class="table-set">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach ($episodes as $episode)
                            <tr data-id="{{$episode->id}}">
                                <td><input type="checkbox" value="{{$episode->id}}" class="checked_id"
                                           name="checked_id[]"/></td>
                                <td>{{$episode->id}}</td>
                                <td>
                                    {{$episode->name}}
                                </td>

                                <td>
                                    {{$episode->author}}
                                </td>

                                <td>
                                    <a href="{{route('casts.episode.index', ['category_id' => $episode->category->id])}}">
                                        {{$episode->category->name}}
                                    </a>
                                </td>
                                <td class="am-hide-sm-only">
                                    {{$episode->created_at}}
                                </td>

                                <td class="am-hide-sm-only">
                                    {!! is_something('is_free', $episode) !!}
                                </td>
                                <td>
                                    <div class="am-btn-toolbar">
                                        <div class="am-btn-group am-btn-group-xs">
                                            <a class="am-btn am-btn-default am-btn-xs am-text-primary" target="_blank" href="http://{{env('HOME_DOMAIN').'/episode/'. $episode->id}}">
                                                <span class="am-icon-eye"></span> 查看
                                            </a>
                                        </div>
                                    </div>
                                </td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>

                    共 {{$episodes->total()}} 条记录

                    <div class="am-cf">
                        <div class="am-fr">
                            {!! $episodes->links() !!}
                        </div>
                    </div>
                    <hr/>
                </form>
            </div>

        </div>

    </div>
@endsection

@section('js')
    <script>
        $(function () {


            //是否...
            $(".is_something").click(function () {
                var _this = $(this);
                var data = {
                    id: _this.parents("tr").data('id'),
                    attr: _this.data('attr')
                }

                $.ajax({
                    type: "PATCH",
                    url: "/casts/episode/is_something",
                    data: data,
                    success: function (data) {
                        if (data.status == 0) {
                            alert(data.msg);
                            return false;
                        }
                        _this.toggleClass('am-icon-close am-icon-check');
                    }
                });
            });

            //切换栏目
            $("#change_category").change(function () {
                var category_id = $(this).val();
                if (category_id == "-1") {
                    location.href = "{{route('casts.episode.index')}}";
                    return false;
                }
                var url = "/casts/episode?category_id=" + category_id;
                location.href = url;
            })
        });
    </script>
@endsection